package core

import (
	"github.com/kataras/iris/v12"
	"golang.org/x/crypto/bcrypt"
	"gopkg.in/go-playground/validator.v9"
)

func ValidForm(ctx iris.Context, validVar interface{}) (err error) {
	if err := ctx.ReadJSON(validVar); err != nil {
		return err
	} else {
		valid := validator.New()
		if err := valid.Struct(validVar); err != nil {
			return err
		} else {
			return nil
		}
	}
}

func EncryptPassword(pwd string) (string, error) {
	if hash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost); err != nil {
		return pwd, err
	} else {
		return string(hash), nil
	}
}

func VerifyPassword(enPwd, pwd string) error {
	return bcrypt.CompareHashAndPassword([]byte(enPwd), []byte(pwd))
}
